package com.ruoyi.web.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.config.pay.CommonFieldName;
import com.ruoyi.common.config.pay.OrderIdGenerator;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.web.request.IESI123102Request;
import com.ruoyi.web.request.IESI123103Request;
import com.ruoyi.web.service.AgentPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.PaymentApplyMapper;
import com.ruoyi.web.domain.PaymentApply;
import com.ruoyi.web.service.IPaymentApplyService;
import com.ruoyi.common.core.text.Convert;

/**
 * 代付申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-04
 */
@Service
public class PaymentApplyServiceImpl implements IPaymentApplyService 
{
    @Autowired
    private PaymentApplyMapper paymentApplyMapper;
    @Autowired
    private AgentPayService agentPayService;
    /**
     * 查询代付申请
     * 
     * @param sequenceNo 代付申请ID
     * @return 代付申请
     */
    @Override
    public PaymentApply selectPaymentApplyById(String sequenceNo)
    {
        return paymentApplyMapper.selectPaymentApplyById(sequenceNo);
    }

    /**
     * 查询代付申请列表
     * 
     * @param paymentApply 代付申请
     * @return 代付申请
     */
    @Override
    public List<PaymentApply> selectPaymentApplyList(PaymentApply paymentApply)
    {
        return paymentApplyMapper.selectPaymentApplyList(paymentApply);
    }

    /**
     * 新增代付申请
     * 
     * @param paymentApply 代付申请
     * @return 结果
     */
    @Override
    public int insertPaymentApply(PaymentApply paymentApply) throws Exception
    {
        BusinessException.notNull(paymentApply.getPayeeAcct(),"请填写收款账号");
        BusinessException.notNull(paymentApply.getPayeeNm(),"请填写收收款方户名");
        IESI123103Request request =new IESI123103Request();
        request.setAmt(paymentApply.getAmt());
        request.setCnt(paymentApply.getCnt());
        request.setAddInfo(paymentApply.getAddInfo());
        request.setFileId(paymentApply.getFileId());
        request.setMode(paymentApply.getMode());
        request.setType("T1");
        request.setFileName(paymentApply.getFileName());
        request.setPayeeAcct(paymentApply.getPayeeAcct());
        request.setFileId(paymentApply.getFileId());
        request.setPayeeBranchCd(paymentApply.getPayeeBranchCd());
        request.setPayeeBranchNm(paymentApply.getPayeeBranchNm());
        request.setPayeeNm(paymentApply.getPayeeNm());
        request.setPayeeType(paymentApply.getPayeeType());
        paymentApply.setSequenceNo(OrderIdGenerator.getDefault().nextId());
        HashMap<String, Object> resultMap= agentPayService.IESI123103(request,paymentApply.getSequenceNo(),paymentApply.getMerchCd());
        String respCode = (String) resultMap.get(CommonFieldName.RESP_CODE);
        String respMsg = (String) resultMap.get(CommonFieldName.RESP_MSG);
        paymentApply.setStatus(respCode);
        paymentApply.setMsg(respMsg);
        paymentApply.setType("T1");
        return paymentApplyMapper.insertPaymentApply(paymentApply);
    }

    /**
     * 修改代付申请
     * 
     * @param paymentApply 代付申请
     * @return 结果
     */
    @Override
    public int updatePaymentApply(PaymentApply paymentApply)
    {
        return paymentApplyMapper.updatePaymentApply(paymentApply);
    }

    /**
     * 删除代付申请对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaymentApplyByIds(String ids)
    {
        return paymentApplyMapper.deletePaymentApplyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除代付申请信息
     * 
     * @param sequenceNo 代付申请ID
     * @return 结果
     */
    @Override
    public int deletePaymentApplyById(String sequenceNo)
    {
        return paymentApplyMapper.deletePaymentApplyById(sequenceNo);
    }
}
